package com.project.weimi.activity;

import com.project.weimi.R;
import com.project.weimi.application.AppManager;
import com.project.weimi.fragment.HomeFragment;
import com.project.weimi.fragment.LifeFragment;
import com.project.weimi.fragment.MineFragment;
import com.project.weimi.fragment.ShoppingFragment;
import com.project.weimi.utils.SetClickListener;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends BaseActivity {

	private TextView tv_home, tv_life, tv_shopping, tv_mine;
	private HomeFragment homeFragment;
	private LifeFragment lifeFragment;
	private ShoppingFragment shopFragment;
	private MineFragment mineFragment;

	private FragmentManager manager;
	private FragmentTransaction transaction;
	// 记录当前页
	public static int currentPage = 0;
	private long exitTime = 0;
	
	
	public ShoppingFragment getShopFragment() {
		return shopFragment;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tintManager.setStatusBarTintResource(R.color.main_titleColor);
		
		initView();
		initFragment();

		// 获得从“我的积分”,"通用设置"页面传来的intent
		int fragId = getIntent().getIntExtra("fragId", -1);
		//Log.i("aaa", "fragId的值====="+fragId);
		
		if(fragId > 0){
			if(fragId == R.id.tv_home){
				showFragment(0);
			}
		}
		
		
		
		
	}

	private void initView() {
		tv_home = (TextView) findViewById(R.id.tv_home);
		tv_life = (TextView) findViewById(R.id.tv_life);
		tv_shopping = (TextView) findViewById(R.id.tv_shopping);
		tv_mine = (TextView) findViewById(R.id.tv_mine);

		setTab(0);
		
		// 监听
		SetClickListener.setClickListener(listener, tv_home, tv_life, tv_shopping, tv_mine);
	}

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.tv_home: // 首页
				showFragment(0);
				
				setTab(0);
				break;
			case R.id.tv_life: // 社区
				showFragment(1);
				
				setTab(1);
				break;
			case R.id.tv_shopping: // 购物车
				showFragment(2);
				
				setTab(2);
				break;
			case R.id.tv_mine: // 我的
				showFragment(3);
				
				setTab(3);
				break;
			default:
				break;
			}
		}
	};

	// 初始化Fragment，在Fragmentmanager里根据Tag去找相应的Fragment
	// 屏幕发生旋转，重新调用onCreate方法，否则会发生重叠
	private void initFragment() {
		manager = getSupportFragmentManager();
		homeFragment = (HomeFragment) manager.findFragmentByTag("homeFragment");
		lifeFragment = (LifeFragment) manager.findFragmentByTag("lifeFragment");
		shopFragment = (ShoppingFragment) manager.findFragmentByTag("shopFragment");
		mineFragment = (MineFragment) manager.findFragmentByTag("mineFragment");
		showFragment(currentPage);
	}

	// 显示Fragment
	private void showFragment(int position) {
		transaction = manager.beginTransaction();
		hideFragment(transaction);
		switch (position) {
		case 0:
			if (homeFragment == null) {
				homeFragment = new HomeFragment();
				transaction.add(R.id.root_frameLayout, homeFragment, "homeFragment");
			} else {
				transaction.show(homeFragment);
			}
			
			break;
		case 1:
			if (lifeFragment == null) {
				lifeFragment = new LifeFragment();
				transaction.add(R.id.root_frameLayout, lifeFragment, "lifeFragment");
			} else {
				transaction.show(lifeFragment);
			}
			
			break;
		case 2:
			if (shopFragment == null) {
				shopFragment = new ShoppingFragment();
				transaction.add(R.id.root_frameLayout, shopFragment, "shopFragment");
			} else {
				transaction.show(shopFragment);
			}
			
			break;
		case 3:
			if (mineFragment == null) {
				mineFragment = new MineFragment();
				transaction.add(R.id.root_frameLayout, mineFragment, "mineFragment");
			} else {
				transaction.show(mineFragment);
			}
			
			break;
		default:
			break;
		}
		transaction.commit();
	}

	// 隐藏Fragment碎片，避免重复
	private void hideFragment(FragmentTransaction transaction) {
		if (homeFragment != null) {
			transaction.hide(homeFragment);
		}
		if (lifeFragment != null) {
			transaction.hide(lifeFragment);
		}
		if (shopFragment != null) {
			transaction.hide(shopFragment);
		}
		if (mineFragment != null) {
			transaction.hide(mineFragment);
		}
	}

	private void setTab(int index) {
		// TODO Auto-generated method stub
		Drawable rightDrawable = null;
		switch (index) {
		case 0:
			tv_home.setTextColor(Color.parseColor("#945DC1"));
			rightDrawable = getResources().getDrawable(R.drawable.icon_home_pre);  
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());  
            tv_home.setCompoundDrawables(null, rightDrawable, null, null);
			
			tv_life.setTextColor(Color.parseColor("#8C8C8C"));
			rightDrawable = getResources().getDrawable(R.drawable.icon_life);  
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
            tv_life.setCompoundDrawables(null, rightDrawable, null, null);

			tv_shopping.setTextColor(Color.parseColor("#8C8C8C"));
			rightDrawable = getResources().getDrawable(R.drawable.icon_shopping);  
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
            tv_shopping.setCompoundDrawables(null, rightDrawable, null, null);

			tv_mine.setTextColor(Color.parseColor("#8C8C8C"));
			rightDrawable = getResources().getDrawable(R.drawable.icon_mine);  
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
			tv_mine.setCompoundDrawables(null, rightDrawable, null, null);
			break;
		case 1:
			tv_home.setTextColor(Color.parseColor("#8C8C8C"));
			rightDrawable = getResources().getDrawable(R.drawable.icon_home);  
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
			tv_home.setCompoundDrawables(null, rightDrawable, null, null);
			
			tv_life.setTextColor(Color.parseColor("#945DC1"));
			rightDrawable = getResources().getDrawable(R.drawable.icon_life_pre);  
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
            tv_life.setCompoundDrawables(null, rightDrawable, null, null);
			
			tv_shopping.setTextColor(Color.parseColor("#8C8C8C"));
			rightDrawable = getResources().getDrawable(R.drawable.icon_shopping);  
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
            tv_shopping.setCompoundDrawables(null, rightDrawable, null, null);
			
			tv_mine.setTextColor(Color.parseColor("#8C8C8C"));
			rightDrawable = getResources().getDrawable(R.drawable.icon_mine);  
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
            tv_mine.setCompoundDrawables(null, rightDrawable, null, null);
			break;
		case 2:
			tv_home.setTextColor(Color.parseColor("#8C8C8C"));
			rightDrawable = getResources().getDrawable(R.drawable.icon_home);  
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
			tv_home.setCompoundDrawables(null, rightDrawable, null, null);
			
			tv_life.setTextColor(Color.parseColor("#8C8C8C"));
			rightDrawable = getResources().getDrawable(R.drawable.icon_life);  
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
            tv_life.setCompoundDrawables(null, rightDrawable, null, null);
			
			tv_shopping.setTextColor(Color.parseColor("#945DC1"));
			rightDrawable = getResources().getDrawable(R.drawable.icon_shopping_pre);  
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
            tv_shopping.setCompoundDrawables(null, rightDrawable, null, null);
			
			tv_mine.setTextColor(Color.parseColor("#8C8C8C"));
			rightDrawable = getResources().getDrawable(R.drawable.icon_mine);  
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
            tv_mine.setCompoundDrawables(null, rightDrawable, null, null);
			break;
		case 3:
			tv_home.setTextColor(Color.parseColor("#8C8C8C"));
			rightDrawable = getResources().getDrawable(R.drawable.icon_home);  
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
			tv_home.setCompoundDrawables(null, rightDrawable, null, null);
			
			tv_life.setTextColor(Color.parseColor("#8C8C8C"));
			rightDrawable = getResources().getDrawable(R.drawable.icon_life);  
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
			tv_life.setCompoundDrawables(null, rightDrawable, null, null);
			
			tv_shopping.setTextColor(Color.parseColor("#8C8C8C"));
			rightDrawable = getResources().getDrawable(R.drawable.icon_shopping);  
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
            tv_shopping.setCompoundDrawables(null, rightDrawable, null, null);
			
            tv_mine.setTextColor(Color.parseColor("#945DC1"));
			rightDrawable = getResources().getDrawable(R.drawable.icon_mine_pre);  
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
			tv_mine.setCompoundDrawables(null, rightDrawable, null, null);
			break;
		}
	}

	// 双击退出程序
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exit();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	public void exit(){
		if(System.currentTimeMillis() - exitTime > 2000){
			Toast.makeText(getApplicationContext(), "再按一次退出程序",
					Toast.LENGTH_SHORT).show();
			exitTime = System.currentTimeMillis();
		}else{
			AppManager.getAppManager().AppExit(this);
		}
	}
	
//	@Override
//	void setStatusBar(int statusBarColorRes) {
//		// TODO Auto-generated method stub
//		super.setStatusBar(R.color.main_titleColor);
//	}
	
}
